package com.ca.ridersclubapplication.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;

import com.ca.ridersclubapplication.AccountActivity;
import com.ca.ridersclubapplication.PaperActivity;
import com.ca.ridersclubapplication.R;
import com.ca.ridersclubapplication.data.bean.PaperInfo;
import com.ca.ridersclubapplication.request.FileRequester;
import com.ca.ridersclubapplication.request.dto.HistoryDto;
import com.ca.ridersclubapplication.request.dto.PaperInfoDto;
import com.ca.ridersclubapplication.util.ImageUtils;
import com.ca.ridersclubapplication.util.TimeUtils;

import java.util.ArrayList;
import java.util.List;

public class PaperHistoryAdapter extends RecyclerView.Adapter<PaperHistoryAdapter.PaperHistoryHolder> {

    private Activity activity;

    private List<HistoryDto> list;

    public PaperHistoryAdapter(List<HistoryDto> list) {
        this.list = list == null ? new ArrayList<>() : list;
    }

    @NonNull
    @Override
    public PaperHistoryHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.paper_history_item_layout, parent, false);
        return new PaperHistoryHolder(view);
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public void onBindViewHolder(@NonNull PaperHistoryHolder holder, int position) {

        HistoryDto dto = list.get(position);

        ImageUtils.loadCircleImage(holder.headerImage.getContext(), FileRequester.HEADER_BY_ID_BASE_URL + dto.getAuthorId(), holder.headerImage);
        holder.nameView.setText(dto.getAuthorName());
        holder.timeView.setText(TimeUtils.parseShowDateTime(dto.getTime()));
        holder.titleView.setText(dto.getTitle());

        //  点击事件
        holder.parent.setOnClickListener(v -> {
            if (activity != null) {
                Intent intent = new Intent(activity, PaperActivity.class);
                intent.putExtra("paperId", dto.getPaperId());
                activity.startActivity(intent);
            }
        });

        holder.headerImage.setOnClickListener(v -> {
            if (activity != null) {
                Intent intent = new Intent(activity, AccountActivity.class);
                intent.putExtra("accountId", dto.getAuthorId());
                activity.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @SuppressLint("NotifyDataSetChanged")
    public void setList(List<HistoryDto> list) {
        this.list = list == null ? new ArrayList<>() : list;
        notifyDataSetChanged();
    }

    @SuppressLint("NotifyDataSetChanged")
    public void appendList(List<HistoryDto> list) {

        if (list == null) {
            list = new ArrayList<>();
        }

        this.list.addAll(list);
        notifyDataSetChanged();
    }

    public static class PaperHistoryHolder extends RecyclerView.ViewHolder {

        public ConstraintLayout parent;
        public ImageView headerImage;
        public TextView nameView;
        public TextView timeView;
        public TextView titleView;

        public PaperHistoryHolder(@NonNull View itemView) {
            super(itemView);

            parent = itemView.findViewById(R.id.cl_parent);
            headerImage = itemView.findViewById(R.id.iv_header);
            nameView = itemView.findViewById(R.id.tv_name);
            timeView = itemView.findViewById(R.id.tv_time);
            titleView = itemView.findViewById(R.id.tv_title);
        }
    }

    public void setActivity(Activity activity) {
        this.activity = activity;
    }
}
